package com.elitel.common.messageQueue;


import com.elitel.base.variable.RabbitMQRoutingKey;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

//@Configuration
public class RabbitMqConfig {

   // @Bean
   // public Queue logQueueDestination() {
   //     return new Queue("logQueueDestination");
   // }

   // @Bean
   // public Queue serverQueueDestination() {
   //     return new Queue("serverQueueDestination");
   // }

   @Bean
   Queue serverTimeDestinationQueue() {
      return new Queue(RabbitMQRoutingKey.serverTimeDestinationQueue, false);
   }

   @Bean
   TopicExchange exchange(){
      return new TopicExchange(RabbitMQRoutingKey.topicDestination);
   }

	@Bean
	Binding binding(Queue queue, TopicExchange exchange) {
		return BindingBuilder.bind(queue).to(exchange).with(RabbitMQRoutingKey.serverTimeDestination);
	}

   @Bean
   SimpleMessageListenerContainer container(ConnectionFactory connectionFactory, MessageListenerAdapter listenerAdapter){
      SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
      container.setConnectionFactory(connectionFactory);
      container.setQueueNames(RabbitMQRoutingKey.serverTimeDestinationQueue);
      container.setMessageListener(listenerAdapter);
      return container;
   }

   @Bean
   MessageListenerAdapter listenerAdapter(RabbitMQReceiver receiver){
      return new MessageListenerAdapter(receiver, "receiveMessage");
   }
}
